#include "registerwindow.h"
#include "ui_registerwindow.h"

RegisterWindow::RegisterWindow(QTcpSocket * clientSocket,QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::RegisterWindow)
    , clientSocket(clientSocket)
{
    ui->setupUi(this);
    QPixmap pixmap("../../../resource/3.jpg");
    // 创建一个QPalette实例
    QPalette palette;
    palette.setBrush(QPalette::Window, pixmap);
    // 设置窗口的调色板
    this->setPalette(palette);
    // 设置窗口标题
    this->setWindowTitle("病人诊断");

    this->ui->label_2->setStyleSheet("QLabel { background-color: white; }");
    this->ui->label_3->setStyleSheet("QLabel { background-color: white; }");
    this->ui->label_4->setStyleSheet("QLabel { background-color: white; }");
    this->ui->label_5->setStyleSheet("QLabel { background-color: white; }");

    // get doctor's id and name
    QString patientsQueryStr = "SELECT DISTINCT P.patient_id , P.name FROM Appointments C  JOIN Patients P ON C.patient_id = P.patient_id  WHERE C.doctor_id = "+QString::number(currentUserID)+";";
    clientSocket->write(patientsQueryStr.toUtf8());
    clientSocket->waitForBytesWritten(1000); // 等待数据写入完成，超时时间为1秒

    if (clientSocket->waitForReadyRead(3000)) { // 等待数据接收，超时时间为3秒
        // 接收数据
        QByteArray data = clientSocket->readAll();

        // 将数据转换为字符串
        QString receivedData = QString::fromUtf8(data);

        // 检查接收到的数据是否为"failed"
        if (receivedData == "failed") {
            // 显示错误消息
            QMessageBox::information(this, tr("Information"), tr("No Patient need to make MedicalRecords."));
        } else {
            QStringList items = receivedData.split('\n');
            for (const QString &item : items) {
                if(item.length()<3)
                    continue;
                ui->doctorBox->addItem(item);
            }
        }
    } else {
        // 如果超时未接收到数据
        QMessageBox::warning(this, tr("Error"), tr("No response from the server."));
        exit(999);
    }
}

void RegisterWindow::on_RegisterButton_clicked(){
    // get select Doctor ID
    QString diagnosisStr = ui->diagnosisEdit->toPlainText();
    QString instructionsStr = ui->instructionsEdit->toPlainText();
    QString prescriptionStr = ui->prescriptionEdit->toPlainText();

    QString selectPatientStr = ui->doctorBox->currentText();
    QString patientIDStr = selectPatientStr.section('|', 0, 0);

    // 获取当前日期时间
    QDateTime currentDateTime = QDateTime::currentDateTime();
    QString formattedDateTime = currentDateTime.toString("yyyy-MM-dd");

    QString queryStr = "INSERT INTO MedicalRecords (patient_id, date, diagnosis, instructions, prescription) \
                            VALUES ("+patientIDStr+", '"+formattedDateTime+"', '"+diagnosisStr+"', '"+instructionsStr+"', '"+prescriptionStr+"');";

    qDebug()<<queryStr;

    clientSocket->write(queryStr.toUtf8());
    clientSocket->waitForBytesWritten(1000); // 等待数据写入完成，超时时间为1秒

    if (clientSocket->waitForReadyRead(3000)) { // 等待数据接收，超时时间为3秒
        // 接收数据
        QByteArray data = clientSocket->readAll();

        // 将数据转换为字符串
        QString receivedData = QString::fromUtf8(data);

        // 检查接收到的数据是否为"failed"
        if (receivedData == "failed") {
            // 显示错误消息
            QMessageBox::information(this, tr("Success"), tr("Already Registered"));
        } else {
            QMessageBox::warning(this,tr("Error"),receivedData);
        }
    } else {
        // 如果超时未接收到数据
        QMessageBox::warning(this, tr("Error"), tr("No response from the server."));
    }
}

void RegisterWindow::on_BackButton_clicked(){
    emit back();
}

RegisterWindow::~RegisterWindow()
{
    delete ui;
}
